package com.android.ailaoshan.index.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.ailaoshan.R;
import com.android.ailaoshan.user.LoginActivity;
import com.android.ailaoshan.user.LoginHandler;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MineFragment extends Fragment {

    private Context mContext;

    private View mineView;
    private QMUIGroupListView groupListView;
    private TextView userName;
    private TextView logout;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();

        mineView = inflater.inflate(R.layout.mine_fragment,container,false);
        return mineView;
    }

    @Override
    public void onStart(){
        super.onStart();

        groupListView = mineView.findViewById(R.id.mine_user_item);
        userName = mineView.findViewById(R.id.user_info_text);
        logout = mineView.findViewById(R.id.user_info_logout);

        initItemList();
    }

    @Override
    public void onResume(){
        super.onResume();

        isUserLogin();
    }

    /**
     * 初始化列表
     */
    private void initItemList(){

        // GPS设置
        QMUICommonListItemView settingGPS = groupListView.createItemView("GPS设置");
        settingGPS.setOrientation(QMUICommonListItemView.VERTICAL);

        // 点击事件
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v instanceof QMUICommonListItemView){
                    CharSequence text = ((QMUICommonListItemView) v).getText();
                    if(text == "GPS设置"){
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(intent, 701);
                    }
                }
            }
        };

        // 显示视图
        QMUIGroupListView.newSection(mContext)
                .setTitle("基本设置")
                .addItemView(settingGPS,onClickListener)
                .addTo(groupListView);

        // 注销按钮点击监听
        logout.setOnClickListener(
                (v) -> {
                    LoginHandler loginHandler = new LoginHandler(mContext);
                    loginHandler.addData("","",0);
                }
        );
    }

    /**
     * 判断用户是否登录
     */
    private void isUserLogin(){
        LoginHandler loginHandler = new LoginHandler(mContext);
        Map<String,Object> userMap = loginHandler.getData();

        if(userMap != null) {
            String userNameTxt = userMap.get("userName").toString();
            String userId = userMap.get("userID").toString();

            // 已登录
            if ( !(userId.equals("0")) ) {
                if( !(userNameTxt.equals("")) && (userName != null) ){
                    userName.setText(userNameTxt);
                    if(logout != null){
                        logout.setVisibility(View.VISIBLE);
                    }
                }
            }else{
                if(logout != null){
                    logout.setVisibility(View.INVISIBLE);
                }
                if(userName != null){
                    userName.setOnClickListener(
                            (v) -> {
                                Intent intent = new Intent(mContext, LoginActivity.class);
                                startActivity(intent);
                            }
                    );
                }
            }
        }
    }
}
